<?php
namespace frontend\controllers;

use common\models\MktCollectSearch;
use common\models\MktPostResponseSearch;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\helpers\Url;
use \yii\helpers\Json;

use common\wosotech\Util;
use common\models\MktUser;
use common\models\WxGh;
use common\models\MktPost;
use common\models\MktIndustry;
use common\models\MktArea;
use common\models\MktSourceRoom;
use common\models\MktSourceGift;
use common\models\MktSourceDonate;
use common\models\MktSourceService;
use common\models\MktSourceMember;
use common\models\MktSourcePromotion;
use common\models\MktChat;
use common\models\MktFeedback;

use common\models\MktUserSearch;
use common\models\MktPostSearch;
use common\models\MktChatSearch;
use common\models\MktPostResponse;


/**
 * Site controller
 */
class WapController extends \common\wosotech\base\Controller
{
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction'
            ],
            'set-locale' => [
                'class' => 'common\actions\SetLocaleAction',
                'locales' => array_keys(Yii::$app->params['availableLocales'])
            ]
        ];
    }

    public function getOpenidUser()
    {
        $openid = \common\wosotech\Util::getSessionOpenid();
        $model = MktUser::findByOpenid($openid);
        if (empty($model)) {
            yii::error("The user does not exists, openid=$openid");
        }
        return $model;
    }
    
    public function getMe()
    {
        return $this->getOpenidUser();        
    }

    public function actionTest()
    {
        $this->layout = false;
        $ghid = WxGh::getDefaultGhId();
        $openid = WxGh::getSessionOpenid();
        yii::warning("openid=$openid");
        return $openid;
    }

    public function actionAreaSubcat() 
    {
        $out = [];
        if (isset($_POST['depdrop_parents'])) {
            $parents = $_POST['depdrop_parents'];
            if ($parents != null) {
                $parent_id = $parents[0];
                $out = MktArea::find()->select(['id', 'title AS name'])->children($parent_id)->asArray()->all(); 
                //yii::error(print_r($out, true));
                return \yii\helpers\Json::encode(['output'=>$out, 'selected'=>empty($out) ? '' : $out[0]['id']]);
            }
        }
        return \yii\helpers\Json::encode(['output'=>'', 'selected'=>'']);
    }

    public function actionIndustrySubcat() 
    {
        $out = [];
        if (isset($_POST['depdrop_parents'])) {
            $parents = $_POST['depdrop_parents'];
            if ($parents != null) {
                $parent_id = $parents[0];
                $out = MktIndustry::find()->select(['id', 'title AS name'])->children($parent_id)->asArray()->all(); 
                return \yii\helpers\Json::encode(['output'=>$out, 'selected'=>empty($out) ? '' : $out[0]['id']]);
            }
        }
        return \yii\helpers\Json::encode(['output'=>'', 'selected'=>'']);
    }

    public function actionCallback()
    {
        //$this->layout = false;
        $wxapp = WxGh::getDefaultGh()->loadWx()->getApplication();
        $oauth = $wxapp->oauth;
        $user = $oauth->user();
        $token = $user->getToken()->toArray();
        $openid = $token['openid'];
        //yii::error($token);
        /*        
        [
            'access_token' => 'xxx',
            'expires_in' => 7200,
            'refresh_token' => 'yyy',
            'openid' => 'oD8xWwg-GJiFi9RLEllEzR1bwJ9A',
            'scope' => 'snsapi_userinfo',
        ]
        */

        if ('snsapi_userinfo' == $token['scope']) {
            $originalUser = $user->getOriginal();
            //yii::error($originalUser);
            /*        
            [
                'openid' => 'oD8xWwg-GJiFi9RLEllEzR1bwJ9A',
                'nickname' => 'xx',
                'sex' => 1,
                'language' => 'zh_CN',
                'city' => 'x',
                'province' => 'xx',
                'country' => 'xx',
                'headimgurl' => 'http://wx.qlogo.cn/mmopen/Uf2Tkt1hetGliaFhJPGqIk23ZyE0Y7AFCmefYQAbic2yNRdjO0ZsepFlWA2CHUcewXsqdGIQ0q5nvCIxVJmkAUFzORhqraI5Mp/0',
                'privilege' => [],
            ]
            */
        }

        Yii::$app->session['openid'] = $openid;
        $breakpoint = empty(Yii::$app->session['breakpoint']) ? Url::to(['wap/index'], true) : Yii::$app->session['breakpoint'];
        yii::error("new a openid=$openid, goto breakpoint=$breakpoint");
        return $this->redirect($breakpoint);
        //header('location:'. $breakpoint); exit;
    }

    public function checkUserInfo()
    {
        $me = $this->getMe();
        if (empty($me->mobile)) 
        {
            return $this->redirect(['wap/bindphone']);
        }

        if (empty($me->name) || empty($me->company) || empty($me->duty) || empty($me->industry_id) || empty($me->area_id)) 
        {
            return $this->redirect(['wap/bind-corp']);
        }            
        return true;
    }
    
    public function actionIndex()
    {
        $this->redirect(['market-home', 'page' => 1]);
    }

    public function actionMarketHome()
    {
        yii::error(print_r($_GET, true));
//        if (true !== ($url = $this->checkUserInfo())) return $url;                
        $this->layout = 'mkt_common';
        $searchModel = new MktUserSearch();
        $dataProvider = $searchModel->searchWap(Yii::$app->request->queryParams);
        $industryFirstLevelNodes = MktIndustry::find()->children(0)->all();
        $industryFirstLevelNodes[] = new MktIndustry(['id' =>0, 'title'=>'不限']);
        $areaFirstLevelNodes = MktArea::find()->children(0)->all();
        $areaFirstLevelNodes[] = new MktArea(['id' =>0, 'title'=>'不限']);
        return $this->render('market/home', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'industryFirstLevelNodes' => $industryFirstLevelNodes,
            'areaFirstLevelNodes' => $areaFirstLevelNodes,
        ]);
    }

    public function actionMarketProfile($id)
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        $model = MktUser::findOne($id);
        if ($me->isMyFriend($id)) {
            // 好友详情
            return $this->render('market/profile-friend', [
                'me' => $me,
                'model' => $model,
            ]);
        } else {
            // 男人女人详情
            return $this->render('market/profile', [
                'me' => $me,
                'model' => $model,
            ]);
        }
    }

    public function actionChat($id)
    {
        $this->layout = false;
        $user = MktUser::findOne($id);
        $me = $this->getMe();

        $n = MktChat::updateAll(['is_read' => 1], 'cat = :cat AND recver_id = :recver_id AND sender_id = :sender_id', [':cat' => MktChat::CAT_FRIEND_MESSAGE, ':recver_id'=>$me->id, ':sender_id'=>$user->id]);
        $n = MktChat::updateAll(['is_read' => 1], 'cat = :cat AND recver_id = :recver_id AND sender_id = :sender_id', [':cat' => MktChat::CAT_SYSTEM_MESSAGE, ':recver_id'=>$me->id, ':sender_id'=>$user->id]);
        
        $chats = MktChat::find()
            ->orFilterWhere(['sender_id' => $me->id, 'recver_id' => $user->id])
            ->orFilterWhere(['recver_id' => $me->id, 'sender_id' => $user->id])
            ->orderBy(['id' => SORT_DESC])
            ->limit(20)     // get recent 10 chat messages
            ->all();
        return $this->render('market/chat', [
            'user' => $user,
            'me' => $me,
            'chats' => array_reverse($chats),
        ]);
    }

    // 某人的资源列表
    public function actionMarketList($id)
    {
        $this->layout = 'mkt_base';
        $user = MktUser::findOne($id);
        $members = $user->sourceMembers;
        $rooms = $user->sourceRooms;
        $gifts = $user->sourceGifts;
        return $this->render('market/list', [
            'user' => $user,
            'members' => $members,
            'rooms' => $rooms,
            'gifts' => $gifts,
        ]);
    }

    // 某人的工作圈列表
    public function actionMarketWork($id)
    {
        $this->layout = 'mkt_base';
        $user = MktUser::findOne(['id' => $_GET['id']]);
        $posts = MktPost::find()->where(['user_id' => $_GET['id']])->orderBy(['id' => SORT_DESC])->limit(20)->all();

        return $this->render('market/work', [
                'user' => $user,
                'posts' => $posts,
        ]);
    }

    // 工作圈首页
    public function actionWorkHome()
    {
        $this->layout = 'mkt_common';
        $searchModel = new \common\models\MktPostSearch();
        $dataProvider = $searchModel->searchWap(Yii::$app->request->queryParams);

        return $this->render('work/home', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    //工作圈详情
    public function actionWorkDetail($id)
    {
        $this->layout = 'mkt_base';
        $model = MktPost::findOne($id);
        return $this->render('work/detail', [
            'model' => $model,
            'me' => $this->getMe()
        ]);
    }

    //投诉
    public function actionWorkTousu($id)
    {
        $this->layout = 'mkt_base';
        $model = MktPost::findOne($id);
        return $this->render('work/tousu', [
            'model' => $model,
            'me' => $this->getMe()
        ]);        
    }

    public function actionResourcelibHome()
    {
        $this->layout = 'mkt_common';
        $users = MktUser::find()->where([])->all();
        return $this->render('resourcelib/home', ['users' => $users]);
    }


    //会员资源
    public function actionResourcelibMember()
    {
        $this->layout = 'mkt_common';
        $searchModel = new \common\models\MktSourceMemberSearch();
        $dataProvider = $searchModel->searchWap(Yii::$app->request->queryParams);

        $areaFirstLevelNodes = MktArea::find()->children(0)->all();
        $areaFirstLevelNodes[] = new MktArea(['id' =>0, 'title'=>'不限']);
        return $this->render('resourcelib/member', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'areaFirstLevelNodes' => $areaFirstLevelNodes,
        ]);
    }

    //场地资源
    public function actionResourcelibArea()
    {
        $this->layout = 'mkt_common';
        $searchModel = new \common\models\MktSourceRoomSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $areaFirstLevelNodes = MktArea::find()->children(0)->all();
        $areaFirstLevelNodes[] = new MktArea(['id' =>0, 'title'=>'不限']);
        return $this->render('resourcelib/area', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'areaFirstLevelNodes' => $areaFirstLevelNodes,
        ]);

    }

    //礼品资源
    public function actionResourcelibGift()
    {
        $this->layout = 'mkt_common';
        $searchModel = new \common\models\MktSourceGiftSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('resourcelib/gift', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);

    }

    //现金资源
    public function actionResourcelibCash()
    {
        $this->layout = 'mkt_common';
        $searchModel = new \common\models\MktSourceDonateSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('resourcelib/cash', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    //线上推广
    public function actionResourcelibGeneralize()
    {
        $this->layout = 'mkt_common';
        $searchModel = new \common\models\MktSourcePromotionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $industryFirstLevelNodes = MktIndustry::find()->children(0)->all();
        $industryFirstLevelNodes[] = new MktIndustry(['id' =>0, 'title'=>'不限']);
        return $this->render('resourcelib/generalize', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'industryFirstLevelNodes' => $industryFirstLevelNodes,
        ]);

    }

    //各类服务
    public function actionResourcelibService()
    {
        $this->layout = 'mkt_common';
        $searchModel = new \common\models\MktSourceServiceSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $areaFirstLevelNodes = MktArea::find()->children(0)->all();
        $areaFirstLevelNodes[] = new MktArea(['id' =>0, 'title'=>'不限']);
        return $this->render('resourcelib/service', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'areaFirstLevelNodes' => $areaFirstLevelNodes,
        ]);

    }

    //我
    public function actionSelfHome()
    {
        $this->layout = 'mkt_common';
        $me = $this->getMe();
        if (!empty($jumpUrl = $me->getJump())) {
            return $this->redirect($jumpUrl);
        }
        return $this->render('self/home', [
            'model' => $this->getMe(),
        ]);
    }

    // 我的好友
    public function actionSelfFriendMyfriend()
    {
        $this->layout = 'mkt_top_friend';
        $me = $this->getMe();
        Yii::$app->request->queryParams = yii\helpers\ArrayHelper::merge(Yii::$app->request->queryParams,
            [ 'MktFriendSearch' => ['user_id' => $me->id ]]
        );

        $searchModel = new \common\models\MktFriendSearch();
        $dataProvider = $searchModel->searchWap(Yii::$app->request->queryParams);

        return $this->render('self/friend/myfriend', [
            'me' => $me,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    // 我的好友消息
    public function actionSelfFriendMessage()
    {
        $this->layout = 'mkt_top_friend';
        $me = $this->getMe();
        Yii::$app->request->queryParams = yii\helpers\ArrayHelper::merge(Yii::$app->request->queryParams,
            [ 'MktChatSearch' => [
                'recver_id' => $me->id,
                'cat' => MktChat::CAT_FRIEND_MESSAGE,
            ]]
        );

        $searchModel = new \common\models\MktChatSearch();
        //$dataProvider = $searchModel->searchWap(Yii::$app->request->queryParams);
        $dataProvider = $searchModel->searchWapGroup(Yii::$app->request->queryParams);
        return $this->render('self/friend/message', [
            'me' => $me,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);

    }

    // 好友申请
    public function actionSelfFriendApply()
    {
        $this->layout = 'mkt_top_friend';
        $me = $this->getMe();
        Yii::$app->request->queryParams = yii\helpers\ArrayHelper::merge(Yii::$app->request->queryParams,
            [ 'MktChatSearch' => [
                'recver_id' => $me->id,
                'cat' => MktChat::CAT_ADD_FRIEND,
                'approved' => MktChat::APPROVE_STATUS_WAIT,
                'is_deleted' => 0
            ]]
        );

        $searchModel = new \common\models\MktChatSearch();
        $dataProvider = $searchModel->searchWap(Yii::$app->request->queryParams);

        return $this->render('self/friend/apply', [
            'me' => $me,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    // 我的关注
    public function actionSelfFriendAttention()
    {
        $this->layout = 'mkt_top_friend';
        $me = $this->getMe();
        Yii::$app->request->queryParams = yii\helpers\ArrayHelper::merge(Yii::$app->request->queryParams,
            [ 'MktFollowSearch' => [
                'user_id' => $me->id,
            ]]
        );

        $searchModel = new \common\models\MktFollowSearch();
        $dataProvider = $searchModel->searchWap(Yii::$app->request->queryParams);

        return $this->render('self/friend/attention', [
            'me' => $me,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);

    }

    // 我的贴子
    public function actionSelfPostMypost()
    {
        $this->layout = 'mkt_top_post';
        $me = $this->getMe();
        Yii::$app->request->queryParams = yii\helpers\ArrayHelper::merge(Yii::$app->request->queryParams,
            [ 'MktPostSearch' => [
                'user_id' => $me->id,
            ]]
        );

        $searchModel = new MktPostSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams,
            MktPost::find()->andFilterWhere(['status' => MktPost::POST_STATUS_NORMAL])->orderBy(['id' => SORT_DESC])->limit(2)
        );

        return $this->render('self/post/mypost', [
            'me' => $me,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    // 我的贴子消息
    public function actionSelfPostMessage()
    {
        $this->layout = 'mkt_top_post';
        $me = $this->getMe();
        
        $searchModel = new MktPostResponseSearch();
        $dataProvider = $searchModel->searchWap(Yii::$app->request->queryParams,
            MktPostResponse::find()
                ->joinWith('post')
                ->andWhere([
                    'mkt_post.user_id' => $me->id,
                    'is_done' => 0
                ])
                ->andFilterWhere(['mkt_post.status' => MktPost::POST_STATUS_NORMAL])
                ->orderBy(['id' => SORT_DESC])
        );

        //return $me->getReceivedPostResponseCount();
        return $this->render('self/post/message', [
            'me' => $me,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);

    }

    // 我的收藏贴子
    public function actionSelfPostCollect()
    {
        $this->layout = 'mkt_top_post';
        $me = $this->getMe();
        Yii::$app->request->queryParams = yii\helpers\ArrayHelper::merge(Yii::$app->request->queryParams,
            [ 'MktCollectSearch' => ['user_id' => $me->id ]]
        );

        $searchModel = new MktCollectSearch();
        $dataProvider = $searchModel->searchWap(Yii::$app->request->queryParams);
        return $this->render('self/post/collect', [
            'me' => $me,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);

    }

    // 我的资源
    public function actionSelfResourcesHome()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        return $this->render('self/resources/home', [
            'me' => $me,
        ]);
    }

    public function actionSelfResourcesQrcode()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        return $this->render('self/resources/qrcode', [
            'model' => $me,
        ]);
    }

    public function actionSelfResourcesMember()
    {
        return $this->redirect(['self-resources-qrcode']);
    
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        if (empty($me->sourceMemberCount)) {
            return $this->redirect(['self-resources-addmember']);
        }
        return $this->render('self/resources/member', [
            'me' => $me,
        ]);
    }

    public function actionSelfResourcesAddmember()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        $model = new MktSourceMember();
        $model->loadDefaultValues();     
        $model->user_id = $me->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['self-resources-member']);
        }        
        return $this->render('self/resources/addmember', [
            'me' => $me,
            'model' => $model
        ]);
    }

    public function actionSelfResourcesArea()
    {
        return $this->redirect(['self-resources-qrcode']);
    
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        if (empty($me->sourceRoomCount)) {
            return $this->redirect(['self-resources-addarea']);
        }
        return $this->render('self/resources/area', [
            'me' => $me,
        ]);
    }

    public function actionSelfResourcesAddarea()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        $model = new MktSourceRoom();
        $model->loadDefaultValues();     
        $model->user_id = $me->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['self-resources-area']);
        }        
        return $this->render('self/resources/addarea', [
            'me' => $me,
            'model' => $model
        ]);
    }

    public function actionSelfResourcesGift()
    {
        return $this->redirect(['self-resources-qrcode']);
    
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        if (empty($me->sourceGiftCount)) {
            return $this->redirect(['self-resources-addgift']);
        }
        return $this->render('self/resources/gift', [
            'me' => $me,
        ]);
    }

    public function actionSelfResourcesAddgift()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        $model = new MktSourceGift();
        //$model->loadDefaultValues();     
        $model->user_id = $me->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['self-resources-gift']);
        }        
        return $this->render('self/resources/addgift', [
            'me' => $me,
            'model' => $model
        ]);
    }

    public function actionSelfResourcesCash()
    {
        return $this->redirect(['self-resources-qrcode']);
    
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        if (empty($me->sourceDonateCount)) {
            return $this->redirect(['self-resources-addcash']);
        }
        return $this->render('self/resources/cash', [
            'me' => $me,
        ]);
    }

    public function actionSelfResourcesAddcash()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        $model = new MktSourceDonate();
        //$model->loadDefaultValues();     
        $model->user_id = $me->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['self-resources-cash']);
        }        
        return $this->render('self/resources/addcash', [
            'me' => $me,
            'model' => $model
        ]);
    }

    public function actionSelfResourcesGeneralize()
    {
     //   return $this->redirect(['self-resources-qrcode']);
    
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        if (empty($me->sourcePromotionCount)) {
            return $this->redirect(['self-resources-addgeneralize']);
        }
        return $this->render('self/resources/generalize', [
            'me' => $me,
        ]);
    }

    public function actionSelfResourcesAddgeneralize()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        $model = new MktSourcePromotion();
        $model->loadDefaultValues();     
        $model->user_id = $me->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['self-resources-generalize']);
        }        
        return $this->render('self/resources/addgeneralize', [
            'me' => $me,
            'model' => $model
        ]);
    }

    public function actionSelfResourcesService()
    {
        return $this->redirect(['self-resources-qrcode']);
    
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        if (empty($me->sourceServiceCount)) {
            return $this->redirect(['self-resources-addservice']);
        }
        return $this->render('self/resources/service', [
            'me' => $me,
        ]);
    }

    public function actionSelfResourcesAddservice()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        $model = new MktSourceService();
        $model->loadDefaultValues();     
        $model->user_id = $me->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['self-resources-service']);
        }        
        return $this->render('self/resources/addservice', [
            'me' => $me,
            'model' => $model
        ]);
    }

    // 我的积分
    public function actionSelfMygold()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();        
        $details = $me->getGolds()->orderBy(['id' => SORT_DESC])->limit(20)->all();
        return $this->render('self/mygold', [
            'me' => $me,
            'details' => $details,            
        ]);
    }

    // 个人中心
    public function actionSelfPersonalView()
    {
        $this->layout = 'mkt_base';
        return $this->render('self/personal/view', [
            'model' => $this->getMe(),
        ]);
    }

    // 改名
    public function actionSelfPersonalName()
    {
        $this->layout = 'mkt_base';
        return $this->render('self/personal/name', [
            'model' => $this->getMe(),
        ]);
    }

    // 改机构
    public function actionSelfPersonalCorp()
    {
        $this->layout = 'mkt_base';
        return $this->render('self/personal/corp', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSelfPersonalSex()
    {
        $this->layout = 'mkt_base';
        return $this->render('self/personal/sex', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSelfPersonalHy()
    {
        $this->layout = 'mkt_base';
        $model = $this->getMe();
        if (!empty($model->industry->parent)) {
            $model->parent_industry_id = $model->industry->parent->id;
        }
        return $this->render('self/personal/hy', [
            'model' => $model,
        ]);
    }

    public function actionSelfPersonalQy()
    {
        $this->layout = 'mkt_base';
        $model = $this->getMe();    
        if (!empty($model->area_id)) {
            $model->parent_area_id = $model->area->parent->id;
        }        
        return $this->render('self/personal/qy', [
            'model' => $model,
        ]);
    }

    public function actionSelfPersonalZw()
    {
        $this->layout = 'mkt_base';
        return $this->render('self/personal/zw', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSelfPersonalTelephone()
    {
        $this->layout = 'mkt_base';
        return $this->render('self/personal/telephone', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSelfPersonalEmail()
    {
        $this->layout = 'mkt_base';
        return $this->render('self/personal/email', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSelfPersonalWeixin()
    {
        $this->layout = 'mkt_base';
        return $this->render('self/personal/weixin', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSelfPersonalQq()
    {
        $this->layout = 'mkt_base';
        return $this->render('self/personal/qq', [
            'model' => $this->getMe(),
        ]);
    }

    // 我的设置
    public function actionSettingHome()
    {
        $this->layout = 'mkt_base';
        return $this->render('setting/home', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSettingNews()
    {
        $this->layout = 'mkt_base';
        return $this->render('setting/newsAlert', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSettingDonotdisturb()
    {
        $this->layout = 'mkt_base';
        return $this->render('setting/doNotDisturb', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSettingPrivacy()
    {
        $this->layout = 'mkt_base';
        return $this->render('setting/privacy', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSettingHelp()
    {
        $this->layout = 'mkt_base';
        return $this->render('setting/help', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSettingFeedback()
    {
        $this->layout = false;
        $model = $this->getMe();
        $feedbacks = MktFeedback::find()
            ->where(['user_id' => $model->id])
            ->orderBy(['id' => SORT_DESC])
            ->limit(10)
            ->all();
        return $this->render('setting/feedback', [
            'me' => $model,
            'feedbacks' => array_reverse($feedbacks),
        ]);        
    }

    public function actionSettingAbout()
    {
        $this->layout = 'mkt_base';
        return $this->render('setting/about', [
            'model' => $this->getMe(),
        ]);
    }

    // 我要发贴
    public function actionPostAdd()
    {
        $this->layout = 'mkt_base';
        return $this->render('post/add', [
            'me' => $this->getMe(),
        ]);
    }

    // 发合作贴
    public function actionPostHezuo($id = null)
    {
        $this->layout = 'mkt_base';
        $user = $this->getMe();
        if ($id === null) {
            $model = new MktPost(['scenario' => 'cooperation']);
            $model->loadDefaultValues();    
        } else {
            $model = MktPost::findOne($id);
            $model->loadDefaultValues();      
            $model->start_time = substr($model->start_time,0,10);
            $model->end_time = substr($model->end_time,0,10);            
            if (!empty($model->area_id)) {
                $model->parent_area_id = $model->area->parent->id;
            }            
        }        
        $model->user_id = $user->id;
        $model->post_type = MktPost::POST_TYPE_COOPERATION;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect($id === null ? ['wap/post-add'] : ['wap/self-post-mypost']);
        } else {
            if ($model->hasErrors()) {
                yii::error(print_r($model->getErrors(), true));
            }
            return $this->render('post/hezuo', [
                'model' => $model,
                'me' => $this->getMe(),
            ]);
        }
    }

    // 发招聘贴
    public function actionPostZhaopin($id = null)
    {
        $this->layout = 'mkt_base';
        $user = $this->getMe();
        if ($id === null) {
            $model = new MktPost();
            $model->loadDefaultValues();       
            $model->job_age_min = 18;
            $model->job_age_max = 60;            
        } else {
            $model = MktPost::findOne($id);
            if (!empty($model->area_id)) {
                $model->parent_area_id = $model->area->parent->id;
            }                        
        }                
        $model->user_id = $user->id;
        $model->post_type = MktPost::POST_TYPE_JOB;
        //$model->start_time = date("Y-m-d H:i:s");
        //$model->end_time = date("Y-m-d H:i:s");        
        $model->start_time = date("Y-m-d");
        $model->end_time = date("Y-m-d");
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect($id === null ? ['wap/post-add'] : ['wap/self-post-mypost']);            
        } else {
            if ($model->hasErrors()) {
                yii::error(print_r($model->getErrors(), true));
            }
            return $this->render('post/zhaopin', [
                'model' => $model,
                'me' => $this->getMe(),
            ]);
        }
    }

    // 系统消息
    public function actionSystem()
    {
        $this->layout = false;
        return $this->render('system', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionSmsAjax($mobile) {
        if (empty($mobile))
            return Json::encode(['code' => 1, 'msg' => '手机号不能空号!']);

        if (!preg_match('/^1\d{10}$/', $mobile))
            return Json::encode(['code' => 1, 'msg' => '无效的手机号!']);
            
        $verifyCode = \common\wosotech\Util::randomString();
        yii::warning("verifyCode=$verifyCode");
        if (!Yii::$app->sm->sendVerifyCode($mobile, $verifyCode))       
        {
            return Json::encode(['code' => 1, 'msg' => '验证码发送失败，请稍后重试。']);
        }
        \Yii::$app->cache->set('SMS-VERIFY-CODE' . $mobile, $verifyCode, 15 * 60);
        return Json::encode(['code' => 0]);
    }

    public function actionBindphone()
    {
        $this->layout = 'mkt_base';        
        return $this->render('bind/bindphone', [
            'model' => $this->getMe(),
        ]);
    }

    public function actionBindsucc()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        return $this->render('bind/bindsucc', [
            'model' => $me,
        ]);
    }

    public function actionBindCorp()
    {
        $this->layout = 'mkt_base';
        return $this->render('bind/corp', [
            'model' => $this->getMe(),
        ]);
    }

//http://127.0.0.1/mkt/frontend/web/index.php?r=wap%2Fbind-corpsucc
    public function actionBindCorpsucc()
    {
        $this->layout = 'mkt_base';
        $me = $this->getMe();
        $me->sendTemplateMsgRegisterMember();

        //$postResponse = MktPostResponse::findOne(['id' => 13]);
        //\common\models\WxTemplateId::sendJobPostResponse($postResponse);        
        //return $this->render('bind/corpsucc', [
        //    'model' => $me,
        //]);        
        return $this->render('bind/qrcode', [
            'model' => $me,
        ]);
        
    }

}
